<template>
  <div class="list-common-table">
    <div class="table-container">
      <t-table
        :data="data"
        :columns="COLUMNS"
        :row-key="rowKey"
        :vertical-align="verticalAlign"
        :pagination="pagination"
        :loading="dataLoading"
        :header-affixed-top="headerAffixedTop"
        :stripe="true"
        @page-change="rehandlePageChange"
        @change="rehandleChange"
      >
        <template #op="{ row }">
          <t-space>
            <t-link theme="primary" @click="handleClickDetail(row)">{{ $t('components.commonTable.download') }}</t-link>
          </t-space>
        </template>
      </t-table>
    </div>
  </div>
</template>
<script setup lang="ts">
import { PageInfo, PrimaryTableCol, TableRowData } from 'tdesign-vue-next';
import { computed, onMounted, ref } from 'vue';
import { useRouter } from 'vue-router';

import { getMemberList } from '@/api/member';
import { prefix } from '@/config/global';
import { t } from '@/locales';
import { useSettingStore } from '@/store';

const store = useSettingStore();
const router = useRouter();

const COLUMNS: PrimaryTableCol[] = [
  {
    title: '序号',
    fixed: 'left',
    width: 60,
    ellipsis: true,
    align: 'left',
    colKey: 'serial-number',
  },
  {
    title: '任务名称',
    fixed: 'left',
    width: 400,
    ellipsis: true,
    align: 'left',
    colKey: 'userName',
  },
  {
    title: '创建时间',
    width: 200,
    ellipsis: true,
    colKey: 'registerTime',
  },
  {
    title: '任务状态',
    width: 100,
    colKey: 'status',
  },
  {
    align: 'left',
    fixed: 'right',
    width: 100,
    colKey: 'op',
    title: t('components.commonTable.operation'),
  },
];

const formData = ref({
  userName: '',
  mobilePhone: '',
  gtConsumerCount: '',
  todayConsumer: '',
  level: '1',
});

const rowKey = 'index';
const verticalAlign = 'top' as const;

const pagination = ref({
  defaultPageSize: 50,
  total: 100,
  defaultCurrent: 1,
  page: 1,
  pageSize: 50,
});

const data = ref([]);

const dataLoading = ref(false);
const fetchData = async () => {
  dataLoading.value = true;
  try {
    const res = await getMemberList({
      page: pagination.value.page,
      pageSize: pagination.value.pageSize,
      cardType: 1,
      ...formData.value,
    });
    if (res.code === 0) {
      res.data.userList.map((item: { reduceCount: number }) => {
        item.reduceCount = 1;
        return item;
      });
      data.value = res.data.userList;
      pagination.value = {
        ...pagination.value,
        total: res.data.totalCount,
      };
    }
  } catch (e) {
    console.log(e);
  } finally {
    dataLoading.value = false;
  }
};

onMounted(() => {
  fetchData();
});

const handleClickDetail = (row: any) => {
  router.push(`/member/detail/${row.cardId}`);
};

const rehandlePageChange = (pageInfo: PageInfo, newDataSource: TableRowData[]) => {
  console.log('分页变化', pageInfo, newDataSource);
  pagination.value = {
    ...pagination.value,
    page: pageInfo.current,
    pageSize: pageInfo.pageSize,
  };
  fetchData();
};
const rehandleChange = (changeParams: unknown, triggerAndData: unknown) => {
  console.log('统一Change', changeParams, triggerAndData);
};

const headerAffixedTop = computed(
  () =>
    ({
      offsetTop: store.isUseTabsRouter ? 48 : 0,
      container: `.${prefix}-layout`,
    }) as any, // TO BE FIXED
);
</script>

<style lang="less" scoped>
.list-common-table {
  background-color: var(--td-bg-color-container);
  padding: var(--td-comp-paddingTB-xxl) var(--td-comp-paddingLR-xxl);
  border-radius: var(--td-radius-medium);

  .table-container {
    margin-top: var(--td-comp-margin-xxl);
  }
}

.form-item-content {
  width: 100%;
}

.operation-container {
  display: flex;
  justify-content: flex-end;
  align-items: center;

  .expand {
    .t-button__text {
      display: flex;
      align-items: center;
    }
  }
}

.payment-col {
  display: flex;

  .trend-container {
    display: flex;
    align-items: center;
    margin-left: var(--td-comp-margin-s);
  }
}

.right-handlers-container {
  margin-top: 15px;
  display: flex;
  justify-content: flex-end;
}
.user-tabs {
  margin: 10px 0;
  margin-bottom: 20px;
  display: flex;
  justify-content: space-between;
}
</style>
